#!/usr/bin/env python
#--*-- coding: utf-8 --*--

import sys      # 为了参数
import urllib, urllib2  # 发送URL的包
import demjson  # json的解码、编码
import time
import datetime  # 时间
import tempfile, os
from subprocess import call

url = "http://localhost:8080/task/";

if len(sys.argv) == 1:
    data = {};
    data['name'] = raw_input("请输入名称: \t").strip();
    data['class'] = raw_input("请输入组号: \t").strip();
    data['tags'] = raw_input("请输入标签: \t").strip();
    data['details'] = raw_input("请输入描述: \t").strip();

    data['tags'] = data['tags'].split(",");

    for idx in range(len(data['tags'])):
        data['tags'][idx] = data['tags'][idx].strip();

    data['tags'] = demjson.encode(data['tags']);

    url += "create";
    post_data = urllib.urlencode(data);
    req = urllib2.urlopen(url, post_data);
    content = req.read();
    print("%s\n" % (content));
    exit();

elif sys.argv[1] == "--help" or sys.argv[1] == "-h":
    print("\t请输入参数：");
    print("\t\tcontent [tags]");
    print("\t\tlist [number]");
    exit();

elif sys.argv[1] == "list":
    if len(sys.argv) == 2:
        url += "list/current";
    else:
        url += "/list/" + sys.argv[2];

else:
    url += "/current";

request = urllib2.Request(url)
respose = urllib2.urlopen(request)

if islist:
    list = demjson.decode(respose.read())

    # for idea in list:
    if list.__len__() == 0:
        print("\t没有任何 idea");
        exit();

    for i in range(0, list.__len__())[::-1]:
        task = list[i];
        print("\t%d\t%s\t%s" % (task["id"], task["name"], idea["tags"]));
